package com.wisdom.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.wisdom.http.HttpNewsService;
import com.wisdom.http.HttpRequestUrl;
import com.wisdom.vo.News;

public class ActivityUtil 
{

	private HttpNewsService service;
	private Map<String,String> parmes;
	public static final int COUNT = 20;
	//提示
	public static void toast(Context context,String msg)
	{
		Toast.makeText(context, msg, 1000).show();
	}
	
	public List<News> nextPage(List<News> listOldNews,int startIndex,int endIndex,String title)
	{
		if(listOldNews.size()%COUNT!=0 && listOldNews.size()>COUNT)
		{
			return null;
		}
		service = new HttpNewsService();
		parmes = new HashMap<String, String>();
		if(title!=null){
			parmes.put("news.newsTitle", title);
		}
		parmes.put("news.startIndex", startIndex+"");
		parmes.put("news.endIndex", endIndex+"");	
		String data = service.requestByPost(HttpRequestUrl.url(HttpRequestUrl.NEWS_SELECT), parmes);
		List<News> list = service.parseNews(data);
		if(list.size()>0)
		{
			for(News news:list)
			{
				listOldNews.add(news);
			}
			return listOldNews;
		}
		else
		{
			return null;
		}
	}
	
	//获取IP
	public static String getLocalIpAddress() 
	{  
        String ipaddress="";
        
	    try 
	    {  
	    	for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
	    	{
	    		NetworkInterface intf = en.nextElement();
	    		if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) 
	    		{ 
	    			for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
	    				{
	    				InetAddress inetAddress = enumIpAddr.nextElement();
	    				if (!inetAddress.isLoopbackAddress()) 
	    				{
	    					ipaddress = inetAddress.getHostAddress().toString();
	    					if(!ipaddress.contains("::"))
	    					{
	    						//ipV6的地址[/color]

	    						return ipaddress;
	    					}
	    				}
	    			}
	    		}
	    	}
	        
	        
	    } 
	    catch (SocketException ex) 
	    {  
	        Log.e("WifiPreference IpAddress", ex.toString());  
	    }  
	    return ""; 
    }
	
	//手机信息
	public static String getOSInfo() 
	{	
		/*
		//BOARD 主板
		String phoneInfo = "BOARD: " + android.os.Build.BOARD;
		phoneInfo += ", BOOTLOADER: " + android.os.Build.BOOTLOADER;
		//BRAND 运营商
		phoneInfo += ", BRAND: " + android.os.Build.BRAND;
		phoneInfo += ", CPU_ABI: " + android.os.Build.CPU_ABI;
		phoneInfo += ", CPU_ABI2: " + android.os.Build.CPU_ABI2;
		//DEVICE 驱动
		phoneInfo += ", DEVICE: " + android.os.Build.DEVICE;
		//DISPLAY 显示
		phoneInfo += ", DISPLAY: " + android.os.Build.DISPLAY;
		//指纹
		phoneInfo += ", FINGERPRINT: " + android.os.Build.FINGERPRINT;
		//HARDWARE 硬件
		phoneInfo += ", HARDWARE: " + android.os.Build.HARDWARE;
		phoneInfo += ", HOST: " + android.os.Build.HOST;
		phoneInfo += ", ID: " + android.os.Build.ID;
		//MANUFACTURER 生产厂家
		phoneInfo += ", MANUFACTURER: " + android.os.Build.MANUFACTURER;
		//MODEL 机型
		phoneInfo += ", MODEL: " + android.os.Build.MODEL;
		phoneInfo += ", PRODUCT: " + android.os.Build.PRODUCT;
		phoneInfo += ", RADIO: " + android.os.Build.RADIO;
		phoneInfo += ", RADITAGSO: " + android.os.Build.TAGS;
		phoneInfo += ", TIME: " + android.os.Build.TIME;
		phoneInfo += ", TYPE: " + android.os.Build.TYPE;
		phoneInfo += ", USER: " + android.os.Build.USER;
		//VERSION.RELEASE 固件版本
		phoneInfo += ", VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
		phoneInfo += ", VERSION.CODENAME: " + android.os.Build.VERSION.CODENAME;
		//VERSION.INCREMENTAL 基带版本
		phoneInfo += ", VERSION.INCREMENTAL: " + android.os.Build.VERSION.INCREMENTAL;
		//VERSION.SDK SDK版本
		phoneInfo += ", VERSION.SDK: " + android.os.Build.VERSION.SDK;
		phoneInfo += ", VERSION.SDK_INT: " + android.os.Build.VERSION.SDK_INT;
		*/
		
		String phoneInfo = "BOARD: " + android.os.Build.BOARD;
		phoneInfo += ", MODEL: " + android.os.Build.MODEL;
		phoneInfo += ", VERSION.SDK_INT: " + android.os.Build.VERSION.SDK_INT;
		phoneInfo += ", VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
		
		return phoneInfo;
		
		
		
	}
	
	public static String getProduct() 
	{
		return android.os.Build.PRODUCT;
	}

	public static String getVersion() 
	{
		return android.os.Build.VERSION.RELEASE;
	}
	
}
